package com.NxIndustries.Sapphire;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.htc.configuration.HtcWrapConfiguration;
import com.htc.widget.ActionBarContainer;
import com.htc.widget.ActionBarExt;
import com.htc.widget.ActionBarText;

/**
 * A simple activity to allow the user to manually type in an Intent.
 */
public class CustomShortcutCreatorActivity extends Activity implements View.OnClickListener {
	private ActionBarExt actionBarExt=null;
	private ActionBarText actionBarText=null;    
	private ActionBarContainer actionBarContainer=null;
	
	public static int getHtcThemeID(Context context, int i)
    {
		return HtcWrapConfiguration.getHtcThemeId(context, i);

    }
    private void SetupActionBar() {
        actionBarExt=new ActionBarExt(this,getActionBar());
        actionBarContainer=actionBarExt.getCustomContainer();
        actionBarText=new ActionBarText(this);    		        
	    actionBarText.setPrimaryText("Activity Custom");   
	    actionBarContainer.addCenterView(actionBarText);
    } 

    @Override
    protected void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        Context context = this;
		setTheme(getHtcThemeID(context, 0));
        setContentView(R.layout.custom_shortcut_creator);
        SetupActionBar();
    }
    
    @Override public boolean onCreateOptionsMenu(final Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate( R.menu.shortcut_menu, menu );
        return true;
    }
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.ok:
  		Intent intent = createShortcutIntent();
        setResult(RESULT_OK, intent);
        finish();
  			return true;
      case R.id.cancel:
    	  setResult(RESULT_CANCELED);
          finish();
      }
      return false;
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ok: {
                Intent intent = createShortcutIntent();
                setResult(RESULT_OK, intent);
                finish();
                break;
            }

            case R.id.cancel: {
                setResult(RESULT_CANCELED);
                finish();
                break;
            }
        }
    }

    private Intent createShortcutIntent() {
        Intent intent = new Intent();

        EditText view;
        view = (EditText) findViewById(R.id.action);
        intent.setAction(view.getText().toString());

        view = (EditText) findViewById(R.id.data);
        String data = view.getText().toString();
        view = (EditText) findViewById(R.id.type);
        String type = view.getText().toString();

        boolean dataEmpty = TextUtils.isEmpty(data);
        boolean typeEmpty = TextUtils.isEmpty(type);
        if (!dataEmpty && typeEmpty) {
            intent.setData(Uri.parse(data));
        } else if (!typeEmpty && dataEmpty) {
            intent.setType(type);
        } else if (!typeEmpty && !dataEmpty) {
            intent.setDataAndType(Uri.parse(data), type);
        }

        return new Intent().putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
    }
}